package cn.foolishbird.hamal.frame.queryParam.wrapperHandler;

import cn.foolishbird.hamal.frame.bsassert.BsAssert;
import cn.foolishbird.hamal.frame.bsassert.info.SysInfo;
import cn.foolishbird.hamal.frame.queryParam.QueryOptionTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import java.util.EnumMap;
import java.util.Map;

/**
 * @author Eric
 * @version 1.0
 * @date 2020-09-29
 */
@Service
public class QueryWrapperHandlerManager {

    private static Map<QueryOptionTypeEnum, QueryWrapperHandler> handlers = new EnumMap<>(QueryOptionTypeEnum.class);

    static {
        handlers.put(QueryOptionTypeEnum.EQ, new AbstractQueryWrapperHandler() {
            @Override
            public <T> void doAnd(QueryWrapper<T> queryWrapper, String fieldName, Object v1, Object v2) {
                queryWrapper.eq(fieldName, v1);
            }
        });
        handlers.put(QueryOptionTypeEnum.GT, new AbstractQueryWrapperHandler() {
            @Override
            public <T> void doAnd(QueryWrapper<T> queryWrapper, String fieldName, Object v1, Object v2) {
                queryWrapper.gt(fieldName, v1);
            }
        });
        handlers.put(QueryOptionTypeEnum.GE, new AbstractQueryWrapperHandler() {
            @Override
            public <T> void doAnd(QueryWrapper<T> queryWrapper, String fieldName, Object v1, Object v2) {
                queryWrapper.ge(fieldName, v1);
            }
        });
        handlers.put(QueryOptionTypeEnum.LT, new AbstractQueryWrapperHandler() {
            @Override
            public <T> void doAnd(QueryWrapper<T> queryWrapper, String fieldName, Object v1, Object v2) {
                queryWrapper.lt(fieldName, v1);
            }
        });
        handlers.put(QueryOptionTypeEnum.LE, new AbstractQueryWrapperHandler() {
            @Override
            public <T> void doAnd(QueryWrapper<T> queryWrapper, String fieldName, Object v1, Object v2) {
                queryWrapper.le(fieldName, v1);
            }
        });
        handlers.put(QueryOptionTypeEnum.LIKE, new AbstractQueryWrapperHandler() {
            @Override
            public <T> void doAnd(QueryWrapper<T> queryWrapper, String fieldName, Object v1, Object v2) {
                queryWrapper.like(fieldName, "%" + v1 + "%");
            }
        });
        handlers.put(QueryOptionTypeEnum.BETWEEN, new AbstractQueryWrapperHandler() {
            @Override
            public <T> void doAnd(QueryWrapper<T> queryWrapper, String fieldName, Object v1, Object v2) {
                if (null == v2) {
                    return;
                }
                queryWrapper.between(fieldName, v1, v2);
            }
        });
    }

    public QueryWrapperHandler obtainQueryWrapperHandler(QueryOptionTypeEnum optionTypeEnum) {
        if (null == optionTypeEnum) {
            return null;
        }
        return handlers.get(optionTypeEnum);
    }

}
